$("select.isotope_filter").on("change", function () {
    var c = $('#helptopics_servicecategory').val();
    var s = $('#helptopics_country').val();
    if(c == '*' && s == '*'){
            $('#helptopics_country option').show();
            $('#helptopics_servicecategory option').show();
            var e = '*'; 
    }else if(c == '*' && s != '*'){
        var iscat = $(this).attr('iscat');
        if(iscat == 1){
           var cnt =  $(this).val();
           $('#helptopics_servicecategory option').hide();
           $('#helptopics_servicecategory option.all').show();
           $('ul.topic_tiles li' + cnt).each(function(){
                var cat = $(this).attr('cat');
                $('#helptopics_servicecategory option' + cat).show();
            });
        }else{
            var cnt =  $(this).val();
            $('#helptopics_servicecategory option').hide();
            $('#helptopics_servicecategory option.all').show();
            $('ul.topic_tiles li' + cnt).each(function(){
                var cat = $(this).attr('cat');
                $('#helptopics_servicecategory option' + cat).show();
            });
            //$('#helptopics_servicecategory option').show();
        }
        var e = s;
    }else if(c != '*' && s == '*'){
        var cat = $(this).val();
        var iscat = $(this).attr('iscat');
        if(iscat == 1){
            $('#helptopics_country option').hide();
            $('#helptopics_country option.all').show();
            $('ul.topic_tiles li' + cat).each(function(){
                var cnt = $(this).attr('country');
                $('#helptopics_country option' + cnt).show();
            });
        }else{
            $('#helptopics_servicecategory option').show();
        }
        var e = c;
    }else if(c != '*' && s != '*'){
        var cat = $(this).val();
        var cnt = $(this).val();
        var iscat = $(this).attr('iscat');
        if(iscat == 1 ){
            /*$('#helptopics_country option').hide();
            $('#helptopics_country option.all').show();
            $('ul.topic_tiles li' + cat).each(function(){
                var cnt = $(this).attr('country');
                $('#helptopics_country option' + cnt).show();
            });*/
            $('#helptopics_country option').show();
        }else{
           /*var cnt =  $(this).val();
           $('#helptopics_servicecategory option').hide();
           $('#helptopics_servicecategory option.all').show();
           $('ul.topic_tiles li' + cnt).each(function(){
                var cat = $(this).attr('cat');
                $('#helptopics_servicecategory option' + cat).show();
            });*/
            $('#helptopics_servicecategory option').show();
        }
        var e = c + s;
    }
    $(".isotope_holder ul").isotope({filter: e});
    return !1
});
function prepare_topic(d){
    function rep(i,v){
        $("#topic_"+i).html(v);
    }
    rep('id',d['id']);
    rep('type',d['type']);
    rep('created',d['created']);
    rep('lastupdate',d['lastupdate']);
    rep('expires',d['expires']);
    rep('country',d['country']);
    rep('creator',d['creator']);
    rep('editor',d['editor']);
    rep('timeleft',d['timeleft']);
    rep('subject',d['subject']);
    rep('text',d['text']);
    if(d['to']!='') rep('notes',d['notes']);
    $("#topic_loading").hide();
    $("#topic_body").show();
}
$("a.topics").click(function(){
    var tid = $(this).attr("tid");
    $("#dialog_topic").dialog("open");
    $("#topic_body").hide();
    $("#topic_loading").show();
    window.ajax = $.get('ajax?r=GET_TOPIC&tid='+tid,function(data){
        if((data[0]['data'] === undefined) || ($.type(data) == "string")){
                $("#dialog_topic").dialog("close");
                var data = new Array();
                data[0] = {'error': 500,'data': 'Internal Error, Please call Administrator!\n<!-- SystemErrorDetails: ' + window.data + '-->'};
            }
            $.each(data, function(index, value) {
                if(value['error']){
                    $("#dialog_topic").dialog("close");
                    console.log("Error: " + value['error'] + "\nData: " + value['data'] + "\n");
                    insertStickyError(value['data'],value['error']);
                }else{
                    prepare_topic(value['data']);
                }
            });
    });
});